package com.muse;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import javax.sql.DataSource;
import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: lee
 * Date: 2021/4/16
 * Time: 4:23 下午
 * Description:
 */
@SpringBootApplication
public class Application {

    @Value("${jdbc.url}&zeroDateTimeBehavior=convertToNull")
    private String jdbcUrl;
    @Value("${jdbc.user}")
    private String jdbcUser;
    @Value("${jdbc.pwd}")
    private String jdbcPwd;
    @Value("${jdbc.maxpool:32}")
    private int jdbcMaxPool;

//    @Value("${jedis.maxtotal:16}")
//    private int jedisMaxTotal;
//    @Value("${jedis.addr}")
//    private String jedisAddr;


    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }


    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper()
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                .registerModule(new JavaTimeModule());
    }

    @Bean
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(jdbcUrl);
        if (!StringUtils.isEmpty(jdbcUser)) {
            config.setUsername(jdbcUser);
            config.setPassword(jdbcPwd);
        }
        config.setMaximumPoolSize(jdbcMaxPool);

        return new HikariDataSource(config);
    }

//    @Bean
//    public JedisCluster jedisCluster() {
//        JedisPoolConfig poolConfig = new JedisPoolConfig();
//        poolConfig.setMaxWaitMillis(2000);
//        poolConfig.setMaxTotal(jedisMaxTotal);
//        poolConfig.setMaxIdle(jedisMaxTotal);
//        poolConfig.setTestWhileIdle(true);
//        poolConfig.setMinIdle(0);
//
//        Set<HostAndPort> jedisClusterNodes = new HashSet<>();
//
//        for (String item : jedisAddr.split(",")) {
//            String[] parts = item.split(":");
//            jedisClusterNodes.add(new HostAndPort(parts[0], Integer.parseInt(parts[1])));
//        }
//        return new JedisCluster(jedisClusterNodes, 2000, 10, poolConfig);
//    }
}
